export function extend(to, form) {
    if (!form) return to;
    if (typeof form == 'object') {
        to = to || {};
        for (var item in form) {
            to[item] = form[item];
        }
    }
    return to;
}

//添加事件
export function onFn(element, event, handler) {
    if (document.addEventListener) {
        if (element && event && handler) {
            element.addEventListener(event, handler, false);
        }
    } else {
        if (element && event && handler) {
            element.attachEvent('on' + event, handler);
        }
    }
}
//删除事件
export function offFn(element, event, handler) {
    if (document.addEventListener) {
        if (element && event && handler) {
            element.removeEventListener(event, handler, false);
        }
    } else {
        if (element && event && handler) {
            element.detachEvent('on' + event, handler);
        }
    }
}
//只绑定一次
export function onceFn(el, event, fn) {
    var listener = function () {
        if (fn) {
            fn.apply(this, arguments);
        }
        offFn(el, event, listener);
    };
    onFn(el, event, listener);
}
//判断元素上有没有这个 class
export function hasClass(el, cls) {
    if (!el || !cls) return false;
    if (trim(cls).indexOf(' ') !== -1) throw new Error('className should not contain space.');
    if (el.classList) {
        return el.classList.contains(cls);
    } else {
        return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
}
//添加class
export function addClass(el, cls) {
    if (!el) return;
    var curClass = el.className;
    var classes = trim(cls || '').split(' ');
    for (var i = 0, j = classes.length; i < j; i++) {
        var clsName = classes[i];
        if (!clsName) continue;
        if (el.classList) {
            el.classList.add(clsName);
        } else {
            if (!hasClass(el, clsName)) {
                curClass += ' ' + clsName;
            }
        }
    }
    if (!el.classList) {
        el.className = trim(curClass);
    }

}
//移除对象上的class
export function removeClass(el, cls) {
    if (!el) return;
    var curClass = el.className,
        classes = trim(cls || '').split(' ');
    for (var i = 0, j = classes.length; i < j; i++) {
        var clsName = classes[i];
        if (!clsName) continue;
        if (el.classList) {
            el.classList.remove(clsName);
        } else {
            curClass = curClass.replace(' ' + clsName + ' ', ' ');
        }
    }
    if (!el.classList) el.className = trim(curClass);
}
//去掉前后的空格
export function trim(string) {
    return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
}
export function trimr(string) {
    return (string || '').replace(/[\s\uFEFF]+$/g, '');
}
export function triml(string) {
    return (string || '').replace(/^[\s\uFEFF]+/g, '');
}
/**
 * 
 * t: current time（当前时间）
 * b: beginning value（初始值）
 * c: change in value（变化量）
 * d: duration（持续时间）
 */
export function Linear(t, b, c, d) {
    return (c - b) * t / d + b;
    // return c * t / d + b;
}

export function getBoundingClientRect(element) {
    var rect = element.getBoundingClientRect();
    // whether the IE version is lower than 11
    var isIE = navigator.userAgent.indexOf("MSIE") != -1;
    // fix ie document bounding top always 0 bug
    var rectTop = isIE && element.tagName === 'HTML' ? -element.scrollTop : rect.top;
    return {
        left: rect.left,
        top: rectTop,
        right: rect.right,
        bottom: rect.bottom,
        width: rect.right - rect.left,
        height: rect.bottom - rectTop
    };
}
export function getOffsetRectRelativeToCustomParent(element, parent, fixed) {
    var elementRect = getBoundingClientRect(element), parentRect = getBoundingClientRect(parent);
    if (fixed) {
        var scrollParent = getScrollParent(parent);
        parentRect.top += scrollParent.scrollTop;
        parentRect.bottom += scrollParent.scrollTop;
        parentRect.left += scrollParent.scrollLeft;
        parentRect.right += scrollParent.scrollLeft;
    }

    var rect = {
        top: elementRect.top - parentRect.top,
        left: elementRect.left - parentRect.left,
        bottom: (elementRect.top - parentRect.top) + elementRect.height,
        right: (elementRect.left - parentRect.left) + elementRect.width,
        width: elementRect.width,
        height: elementRect.height
    };
    return rect;
}
export function getScrollParent(element) {
    var parent = element.parentNode;
    if (!parent) { return element; }
    if (parent === document) {
        if (document.body.scrollTop || document.body.scrollLeft) {
            return document.body;
        } else {
            return document.documentElement;
        }
    }

    // Firefox want us to check `-x` and `-y` variations as well
    if (
        ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow')) !== -1 ||
        ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-x')) !== -1 ||
        ['scroll', 'auto'].indexOf(getStyleComputedProperty(parent, 'overflow-y')) !== -1
    ) {

        return parent;
    }
    return getScrollParent(element.parentNode);
}
export function getStyleComputedProperty(element, property) {
    var css = getComputedStyle(element, null);
    return css[property];
}

/**

 */
export function getOffsetParent(element) {
    var offsetParent = element.offsetParent;
    return offsetParent === document.body || !offsetParent ? document.documentElement : offsetParent;
}
export function indexObjectArr(objectArr, val, key) {
    for (let i = 0, m = objectArr.length; i < m; i++) {
        if (objectArr[i][key] == val) return i;
    }
    return -1;
}

export function deepCopy(obj) {
    let result, oClass = getClass(obj)
    if (oClass == "Object") {
        result = {}
    } else if (oClass == "Array") {
        result = []
    } else {
        return obj
    }
    for (let i in obj) {
        let copy = obj[i]
        if (getClass(copy) == "Object") {
            result[i] = deepCopy(copy)
        } else if (getClass(copy) == "Array") {
            result[i] = deepCopy(copy)
        } else {
            result[i] = copy
        }
    }
    return result
}
//判断数据类型
export function getClass(o) {
    return Object.prototype.toString.call(o).slice(8, -1)
}